package com.ozo.cloud.common.base.util;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;

/**
 * 日期工具类
 *
 * @author qiangesoft
 * @date 2024-05-23
 */
public class LocalDateTimeUtils {

    public static final String[] CHINESE_WEEK_DAY = new String[]{"周一", "周二", "周三", "周四", "周五", "周六", "周日"};

    /**
     * 显示到日期
     */
    public static final String FORMAT_DATE = "yyyy-MM-dd";
    /**
     * 显示到小时
     */
    public static final String FORMAT_HOUR = "yyyy-MM-dd HH";
    /**
     * 显示到分
     */
    public static final String FORMAT_MINUTE = "yyyy-MM-dd HH:mm";
    /**
     * 显示到秒
     */
    public static final String FORMAT_SECOND = "yyyy-MM-dd HH:mm:ss";
    /**
     * 显示到毫秒
     */
    public static final String FORMAT_MILLISECOND = "yyyy-MM-dd HH:mm:ss:SSS";
    /**
     * 数字格式显示到日期
     */
    public static final String FORMAT_DATE_NUMBER = "yyyyMMdd";
    /**
     * 数字格式显示到小时
     */
    public static final String FORMAT_HOUR_NUMBER = "yyyyMMddHH";
    /**
     * 数字格式显示到分
     */
    public static final String FORMAT_MINUTE_NUMBER = "yyyyMMddHHmm";
    /**
     * 数字格式显示到秒
     */
    public static final String FORMAT_SECOND_NUMBER = "yyyyMMddHHmmss";
    /**
     * 数字格式显示到毫秒
     */
    public static final String FORMAT_MILLISECOND_NUMBER = "yyyyMMddHHmmssSSS";

    /**
     * 当前时间
     *
     * @return
     */
    public static LocalDateTime now() {
        return LocalDateTime.now();
    }

    /**
     * 当日的开始时间
     *
     * @return
     */
    public static LocalDateTime startOfDay() {
        return now().toLocalDate().atStartOfDay();
    }

    /**
     * 某天的开始时间
     *
     * @param date
     * @return
     */
    public static LocalDateTime startOfDay(LocalDateTime date) {
        return date.toLocalDate().atStartOfDay();
    }

    /**
     * 当日的结束时间
     *
     * @return
     */
    public static LocalDateTime endOfDay() {
        return now().toLocalDate().atTime(LocalTime.MAX);
    }

    /**
     * 某天的结束时间
     *
     * @param date
     * @return
     */
    public static LocalDateTime endOfDay(LocalDateTime date) {
        return date.toLocalDate().atTime(LocalTime.MAX);
    }

    /**
     * 本周第几天
     *
     * @return
     */
    public static int dayOfWeek() {
        return dayOfWeek(now());
    }

    /**
     * 某周第几天
     *
     * @param date
     * @return
     */
    public static int dayOfWeek(LocalDateTime date) {
        DayOfWeek dayOfWeek = date.getDayOfWeek();
        return dayOfWeek.getValue();
    }

    /**
     * 本周星期几
     *
     * @return
     */
    public static String dayNameOfWeek() {
        return dayNameOfWeek(now());
    }

    /**
     * 某周星期几
     *
     * @param date
     * @return
     */
    public static String dayNameOfWeek(LocalDateTime date) {
        int dayNum = dayOfWeek(date);
        return CHINESE_WEEK_DAY[dayNum - 1];
    }

    /**
     * 本周开始时间
     *
     * @return
     */
    public static LocalDateTime startOfWeek() {
        return startOfWeek(now());
    }

    /**
     * 某周开始时间
     *
     * @param date
     * @return
     */
    public static LocalDateTime startOfWeek(LocalDateTime date) {
        return date.minusDays(date.getDayOfWeek().getValue() - 1).toLocalDate().atStartOfDay();
    }

    /**
     * 本周结束时间
     *
     * @return
     */
    public static LocalDateTime endOfWeek() {
        return endOfWeek(now());
    }

    /**
     * 某周结束时间
     *
     * @param date
     * @return
     */
    public static LocalDateTime endOfWeek(LocalDateTime date) {
        return LocalDateTime.of(LocalDate.from(date.plusDays(8 - date.getDayOfWeek().getValue())), LocalTime.MAX);
    }

    /**
     * 当月开始时间
     *
     * @return
     */
    public static LocalDateTime startOfMonth() {
        return startOfMonth(now());
    }

    /**
     * 某月开始时间
     *
     * @param date
     * @return
     */
    public static LocalDateTime startOfMonth(LocalDateTime date) {
        return date.toLocalDate().with(TemporalAdjusters.firstDayOfMonth()).atTime(LocalTime.MIN);
    }

    /**
     * 当月结束时间
     *
     * @return
     */
    public static LocalDateTime endOfMonth() {
        return endOfMonth(now());
    }

    /**
     * 某月结束时间
     *
     * @param date
     * @return
     */
    public static LocalDateTime endOfMonth(LocalDateTime date) {
        return date.toLocalDate().with(TemporalAdjusters.lastDayOfMonth()).atTime(LocalTime.MAX);
    }

    /**
     * 当年开始时间
     *
     * @return
     */
    public static LocalDateTime startOfYear() {
        return startOfYear(now());
    }

    /**
     * 某年开始时间
     *
     * @param date
     * @return
     */
    public static LocalDateTime startOfYear(LocalDateTime date) {
        return date.toLocalDate().with(TemporalAdjusters.firstDayOfYear()).atTime(LocalTime.MIN);
    }

    /**
     * 当年结束时间
     *
     * @return
     */
    public static LocalDateTime endOfYear() {
        return endOfYear(now());
    }

    /**
     * 某年结束时间
     *
     * @param date
     * @return
     */
    public static LocalDateTime endOfYear(LocalDateTime date) {
        return date.toLocalDate().with(TemporalAdjusters.lastDayOfYear()).atTime(LocalTime.MAX);
    }

    /**
     * 当日是否属于时间范围内
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static Boolean between(LocalDateTime startTime, LocalDateTime endTime) {
        return between(now(), startTime, endTime);
    }

    /**
     * 时间范围内
     *
     * @param date
     * @param startTime
     * @param endTime
     * @return
     */
    public static Boolean between(LocalDateTime date, LocalDateTime startTime, LocalDateTime endTime) {
        return date.isAfter(startTime) && date.isBefore(endTime);
    }

    /**
     * 时间格式化
     *
     * @param date
     * @return
     */
    public static String format(LocalDateTime date) {
        return format(date, FORMAT_SECOND);
    }

    /**
     * 时间格式化
     *
     * @param date
     * @param format
     * @return
     */
    public static String format(LocalDateTime date, String format) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        return date.format(formatter);
    }

    /**
     * 字符串转换时间
     *
     * @param date
     * @param format
     * @return
     */
    public static LocalDateTime parse(String date, String format) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        return LocalDateTime.parse(date, formatter);
    }

    /**
     * 增加秒
     *
     * @param second
     * @return
     */
    public static LocalDateTime addSecond(int second) {
        return addSecond(now(), second);
    }

    /**
     * 增加秒
     *
     * @param date
     * @param second
     * @return
     */
    public static LocalDateTime addSecond(LocalDateTime date, int second) {
        return date.plusSeconds(second);
    }

    /**
     * 增加分钟
     *
     * @param minute
     * @return
     */
    public static LocalDateTime addMinute(int minute) {
        return addMinute(now(), minute);
    }

    /**
     * 增加分钟
     *
     * @param date
     * @param minute
     * @return
     */
    public static LocalDateTime addMinute(LocalDateTime date, int minute) {
        return date.plusMinutes(minute);
    }

    /**
     * 增加小时
     *
     * @param hour
     * @return
     */
    public static LocalDateTime addHour(int hour) {
        return addHour(now(), hour);
    }

    /**
     * 增加小时
     *
     * @param date
     * @param hour
     * @return
     */
    public static LocalDateTime addHour(LocalDateTime date, int hour) {
        return date.plusHours(hour);
    }

    /**
     * 增加天
     *
     * @param day
     * @return
     */
    public static LocalDateTime addDay(int day) {
        return addDay(now(), day);
    }

    /**
     * 增加天
     *
     * @param date
     * @param day
     * @return
     */
    public static LocalDateTime addDay(LocalDateTime date, int day) {
        return date.plusDays(day);
    }

    /**
     * 增加周
     *
     * @param week
     * @return
     */
    public static LocalDateTime addWeek(int week) {
        return addWeek(now(), week);
    }

    /**
     * 增加周
     *
     * @param date
     * @param week
     * @return
     */
    public static LocalDateTime addWeek(LocalDateTime date, int week) {
        return date.plusWeeks(week);
    }

    /**
     * 增加月
     *
     * @param month
     * @return
     */
    public static LocalDateTime addMonth(int month) {
        return addMonth(now(), month);
    }

    /**
     * 增加月
     *
     * @param date
     * @param month
     * @return
     */
    public static LocalDateTime addMonth(LocalDateTime date, int month) {
        return date.plusMonths(month);
    }

    /**
     * 增加年
     *
     * @param year
     * @return
     */
    public static LocalDateTime addYear(int year) {
        return addYear(now(), year);
    }

    /**
     * 增加年
     *
     * @param date
     * @param year
     * @return
     */
    public static LocalDateTime addYear(LocalDateTime date, int year) {
        return date.plusYears(year);
    }

    /**
     * 减少秒
     *
     * @param second
     * @return
     */
    public static LocalDateTime reduceSecond(int second) {
        return reduceSecond(now(), second);
    }

    /**
     * 减少秒
     *
     * @param date
     * @param second
     * @return
     */
    public static LocalDateTime reduceSecond(LocalDateTime date, int second) {
        return date.minusSeconds(second);
    }

    /**
     * 减少分钟
     *
     * @param minute
     * @return
     */
    public static LocalDateTime reduceMinute(int minute) {
        return reduceMinute(now(), minute);
    }

    /**
     * 减少分钟
     *
     * @param date
     * @param minute
     * @return
     */
    public static LocalDateTime reduceMinute(LocalDateTime date, int minute) {
        return date.minusMinutes(minute);
    }

    /**
     * 减少小时
     *
     * @param hour
     * @return
     */
    public static LocalDateTime reduceHour(int hour) {
        return reduceHour(now(), hour);
    }

    /**
     * 减少小时
     *
     * @param date
     * @param hour
     * @return
     */
    public static LocalDateTime reduceHour(LocalDateTime date, int hour) {
        return date.minusHours(hour);
    }

    /**
     * 减少天
     *
     * @param day
     * @return
     */
    public static LocalDateTime reduceDay(int day) {
        return reduceDay(now(), day);
    }

    /**
     * 减少天
     *
     * @param date
     * @param day
     * @return
     */
    public static LocalDateTime reduceDay(LocalDateTime date, int day) {
        return date.minusDays(day);
    }

    /**
     * 减少周
     *
     * @param week
     * @return
     */
    public static LocalDateTime reduceWeek(int week) {
        return reduceWeek(now(), week);
    }

    /**
     * 减少周
     *
     * @param date
     * @param week
     * @return
     */
    public static LocalDateTime reduceWeek(LocalDateTime date, int week) {
        return date.minusWeeks(week);
    }

    /**
     * 减少月
     *
     * @param month
     * @return
     */
    public static LocalDateTime reduceMonth(int month) {
        return reduceMonth(now(), month);
    }

    /**
     * 减少月
     *
     * @param date
     * @param month
     * @return
     */
    public static LocalDateTime reduceMonth(LocalDateTime date, int month) {
        return date.minusMonths(month);
    }

    /**
     * 减少年
     *
     * @param year
     * @return
     */
    public static LocalDateTime reduceYear(int year) {
        return reduceYear(now(), year);
    }

    /**
     * 减少年
     *
     * @param date
     * @param year
     * @return
     */
    public static LocalDateTime reduceYear(LocalDateTime date, int year) {
        return date.minusYears(year);
    }

}

